package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.WcBasicInfo;
import com.ruoyi.system.domain.WcWorkInfo;
import com.ruoyi.system.dto.BankReportEx;
import com.ruoyi.system.dto.WorkReportEx;
import com.ruoyi.system.service.IWcBasicInfoService;
import com.ruoyi.system.service.IWcWorkInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WorkReport;
import com.ruoyi.system.service.IWorkReportService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-03-26
 */
@Controller
@RequestMapping("/system/workReport")
public class WorkReportController extends BaseController
{
    private String prefix = "system/workReport";

    @Autowired
    private IWorkReportService workReportService;

    @Autowired
    IWcBasicInfoService wcBasicInfoService;

    @Autowired
    IWcWorkInfoService wcWorkInfoService;

    @RequiresPermissions("system:workReport:view")
    @GetMapping()
    public String workReport()
    {
        return prefix + "/workReport";
    }

    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:workReport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody WorkReport workReport)
    {
        startPage();
        List<WorkReport> list = workReportService.selectWorkReportList(workReport);
        List<WorkReportEx> list2 = new ArrayList<>();
        list.stream().forEach(element -> {
            WorkReportEx workReportEx = new WorkReportEx();
            BeanUtils.copyProperties(element, workReportEx);

            if(element.getBaseinfoid() != null) {
                WcBasicInfo wcBasicInfo = wcBasicInfoService.selectWcBasicInfoById(element.getBaseinfoid());
                if (wcBasicInfo != null) {
                    BeanUtils.copyProperties(wcBasicInfo, workReportEx);
                }
            }

            if(element.getWorkinfoid() != null) {
                WcWorkInfo wcWorkInfo = wcWorkInfoService.selectWcWorkInfoById(element.getWorkinfoid());
                if(wcWorkInfo != null) {
                    BeanUtils.copyProperties(wcWorkInfo, workReportEx);
                }
            }

            list2.add(workReportEx);
        });

        return getDataTable(list2);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:workReport:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkReport workReport)
    {
        List<WorkReport> list = workReportService.selectWorkReportList(workReport);
        ExcelUtil<WorkReport> util = new ExcelUtil<WorkReport>(WorkReport.class);
        return util.exportExcel(list, "workReport");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:workReport:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkReport workReport)
    {
        return toAjax(workReportService.insertWorkReport(workReport));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WorkReport workReport = workReportService.selectWorkReportById(id);
        mmap.put("workReport", workReport);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:workReport:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkReport workReport)
    {
        return toAjax(workReportService.updateWorkReport(workReport));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:workReport:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workReportService.deleteWorkReportByIds(ids));
    }
}
